package com.dapeis.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.ag.dao.DBService;
import com.ag.dao.IDao;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.dao.IUserThirdPartyLoginMapper;
import com.dapeis.domain.UserThirdPartyLogin;

@Service
public class UserThirdPartyLoginService extends DBService<UserThirdPartyLogin> {
    @Resource
    IUserThirdPartyLoginMapper mapper;

    public UserThirdPartyLogin getUserThirdPartyLogin(int type, String pk) {
	Example example = example();
	example.createCriteria().andEqualTo("type", type).andEqualTo("pk", pk);
	return selectOneByExample(example);
    }

    public UserThirdPartyLogin getQQUserThirdPartyLogin(String openId) {
	return getUserThirdPartyLogin(StylistConstants.UserSource.QQ, openId);
    }

    public UserThirdPartyLogin getWxUserThirdPartyLogin(String openId) {
	return getUserThirdPartyLogin(StylistConstants.UserSource.WEIXIN, openId);
    }

    public UserThirdPartyLogin getUserThirdPartyLoginByUserId(Integer userId) {
	Example example = example();
	example.createCriteria().andEqualTo("userId", userId);
	return selectOneByExample(example);
    }

    public boolean addUserThirdPartyLogin(Integer userId, String openId,
	    int type, String ext) {
	UserThirdPartyLogin us = new UserThirdPartyLogin();
	us.setUserId(userId);
	us.setPk(openId);
	us.setType(type);
	us.setExt(ext);
	us.setRecordCreateTime(System.currentTimeMillis() / 1000);
	return insertSelective(us) > 0;
    }

    @Override
    protected IDao<UserThirdPartyLogin> getIDao() {
	return mapper;
    }
}